package com.rem.concurrency.juc.my8Bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列
 * 方式       抛出异常 有返回值,不抛出异常 阻塞等待 超时等待
 * 添加       add      offer()          put()   offer(,,)
 * 移除       remove   poll()           take()  poll(,)
 * 检测队首元素 element peek              -       -
 *
 * @author Rem
 * @date 2021-03-09
 */

public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        test3();
    }


    /**
     * 抛出异常
     */
    public static void test1() {
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));

        //java.lang.IllegalStateException: Queue full
        //System.out.println(blockingQueue.add("d"));

        System.out.println("===============");


        //队列中第一个元素
        String element = blockingQueue.element();
        System.err.println(element);

        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());


        //java.util.NoSuchElementException  队列无元素 抛出异常
        //System.out.println(blockingQueue.remove());
    }


    /**
     * 有返回值 不抛出异常
     */
    public static void test2() {
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));

        //false
        //System.out.println(blockingQueue.offer("d"));


        //队列中第一个元素
        String element = blockingQueue.element();
        System.err.println(element);

        System.out.println("===============");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());

        //null
        //System.out.println(blockingQueue.poll());
    }


    public static void test3() throws InterruptedException {
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);

        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");

        //队列没有位置了，一直阻塞
        // blockingQueue.put("d");

        System.out.println("===============");

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        // 拿不到数据 也会一直阻塞
        // System.out.println(blockingQueue.take());
    }

    public static void test4() throws InterruptedException {
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue(3);

        System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("b", 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("c", 2, TimeUnit.SECONDS));

        //超时2s就退出  打印false
        // System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS));

        System.out.println("===============");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());

        //超时2s就退出 null
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
    }
}


